<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>展示地址</title>
    <link rel="stylesheet" href="/js/bootstrap.min.css">
    <script src="/js/jquery-3.2.1.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var address = $("#address");
            var userid = null;

            //点击事件
            $("#button").click(function () {
                userid = $("#userid").val();
                show();
                $("#adda").attr("href", "AddAddress.html?userid=" + userid);
                return false;
            });

            function show() {
                $.get("/applets/address/selectAddress", {"userid": userid}, function (da) {
                    if (da.code != 0) {
                        alert(da.msg);
                        return;
                    }
                    $.each(da.data, function (i, d) {
                        address.append("<tr><td>" + d.id + "</td><td>" + d.tag +
                            "</td><td>" + d.addr + "</td><td>" + d.userid + "</td><td>" + d.telephone + "</td><td>" +
                            d.receivename + "</td><td><a class='btn btn-primary btn-sm' href='/applets/address/select?id=" + d.id +
                            "'>更新</a>&nbsp;<button type='button' class='btn btn-primary btn-sm' id='del' code='" + d.id +
                            "'>删除</button>&nbsp;<button type='button' class='btn btn-primary btn-sm' id='default' suid='" + d.id +
                            "'>默认地址</button></td></tr>")
                    })
                });
            };
            var defid = null;
            //点击修改默认事件
            address.on("click", '#default', function () {
                defid = $(this).attr("suid");
                userid = $("#userid").val();
                setDefault();
                return false;
            });

            //设为默认
            function setDefault() {
                $.ajax({
                    url: "/applets/address/setDefault",
                    data: {"defid": defid, "userid": userid},
                    type: "post",
                    success: function (ur) {
                        alert(ur.msg);
                    }
                });
            };

            var ids = null;
            //点击删除事件
            address.on("click", '#del', function () {
                ids = $(this).attr("code");
                del();
                return false;
            });

            //删除商品的ajax
            function del() {
                var a = confirm("是否删除该地址?");
                if (!a)
                    return;
                $.ajax({
                    url: "/applets/address/delete",
                    data: {"ids": ids},
                    type: "post",
                    success: function (ur) {
                        alert(ur.msg);
                    }
                });
            };

        });
    </script>
</head>
<body>
<div class="container">
    <h2 class="text-center">地址展示</h2>

    <!--导航-->
    <ul class="nav nav-tabs">
        <li class="nav-item">
            <a class="nav-link active" href="/Menu.html">返回主菜单</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="#" id="adda">新增地址</a>
        </li>
    </ul>

    <!--查询表单-->
    <form action="#" method="post">
        <div class="form-group">
            <label>用户ID</label>
            <input type="number" name="userid" id="userid" class="form-control">
        </div>
        <div>
            <input type="button" value="查找地址" class="form-control" id="button">
        </div>
    </form>
</div>

<div class="container">
    <table class="table table-striped" id="address">
        <thead>
        <tr>
            <th>编号</th>
            <th>标签</th>
            <th>地址</th>
            <th>用户号</th>
            <th>手机号</th>
            <th>收件人</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody></tbody>
    </table>
</div>

</body>
</html>